<template>
  <div class="step">
    <div class="email">
      <p>邮箱号码</p>
      <input type="text"  placeholder="请输入邮箱号码"  @blur="Verifyemail" v-bind:class="{ error: emailFlag }" @focus="reset" v-model="email" />
      <div class="tips" >{{msg}}</div>
      <div class="next" @click="next">发送验证码</div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      flag: true,
      msg: '',
      email: '',
      emailFlag: false
    }
  },
  computed: {
    validate () {
      return !this.msg && this.email
    }
  },
  methods: {

    next () {
      if (this.validate) {
        // console.log(this.email)
        sessionStorage.setItem('email', this.email)
        this.$store.dispatch('user/getEmailData', { email: this.email }).then(res => {
          // // console.log(res)
          this.$emit('next')
        }).catch(res => {
          // console.log(res)
          this.msg = res.message
          this.emailFlag = true
        })
      }
    },
    Verifyemail () {
      const re = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
      if (this.email === '') {
        this.msg = '请输入邮箱'
        this.emailFlag = true
      } else if (re.test(this.email)) {
        this.msg = ''
      } else {
        this.msg = '请输入正确邮箱号码'
        this.emailFlag = true
      }
    },
    reset () {
      this.msg = ''
      this.emailFlag = false
    }
  }
}
</script>

<style lang="scss" scoped>
p {
  display: block;
  height: 22px;
  line-height: 22px;
  color: #3d4966;
  margin-bottom: 10px;
}
input {
  width: 360px;
  height: 44px;
  padding-left: 10px;
  opacity: 1;
  background: #ffffff;
  border: 1px solid #a5aebf;
  border-radius: 4px;

}
.next {
  width: 360px;
  height: 44px;
  opacity: 1;
  background: #304eec;
  border-radius: 4px;
  line-height: 44px;
  color: #ffffff;
  text-align: center;
}
.email {
  display: inline-block;
  margin-top: 272px;
  margin-left: 540px;
  .tips{
    height: 14px;
    margin-top: 8px;
    margin-bottom: 38px;
    color: #f05656;
  }
}
</style>
